package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import org.example.domain.dto.LoginDto;
import org.example.domain.entity.SysUser;
import org.example.domain.model.Result;
import org.example.mapper.SysUserMapper;
import org.example.service.ISysUserService;
import org.example.utils.PasswordEncoder;
import org.springframework.stereotype.Service;

@RequiredArgsConstructor
@Service
public class SysUserServiceImpl implements ISysUserService {

    private SysUserMapper sysUserMapper;
    private PasswordEncoder passwordEncoder;

    @Override
    public Result login(LoginDto loginDto) {
        //验证用户名和密码， 验证通过颁发access_token 和 refresh_token
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", loginDto.getUsername());

        SysUser sysUser = sysUserMapper.selectOne(queryWrapper);

        if (sysUser == null || !passwordEncoder.matches(loginDto.getPassword(), sysUser.getPassword())) {
            return Result.buildFailure("用户名或密码错误");
        }
        return null;
    }
}
